package com.example.Lezedata.ui.activity.address;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.fragment.commonfragment.DateSelect;
import com.example.Lezedata.ui.fragment.commonfragment.GenderSelect;
import com.example.Lezedata.util.GBTimeManager;
import com.example.Lezedata.util.GBUtil;
import com.example.Lezedata.util.StatusBarUtil;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import bid.Bid;
import common.Common;

public class Address_addVc extends GBBaseVc {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_address_add_vc;
    }

    @Override
    public CharSequence title() {
        return "Mailing Address";
    }

    private TextView FirstName;
    private TextView FirstNameInput;

    private TextView LastName;
    private TextView LastNameInput;

    private TextView Phone;
    private TextView PhoneInput;

    private TextView Gender;
    private TextView GenderInput;

    private TextView Birth;
    private TextView BirthInput;

    private TextView Industry;
    private TextView IndustryInput;

    private TextView Region;
    private TextView RegionInput;

    private TextView City;
    private TextView CityInput;

    private TextView Address;
    private TextView AddressInput;

    private TextView CheckBox;

    private  TextView submit;

    private Common.Gender gender_select;
    private String birthDay;
    private String province;
    private String industry;
    private String city;

    private Boolean default_address = true;

    private HashMap<String,ArrayList<String>> cityJson;
    private ArrayList<String> provinceJson;
    private ArrayList<String> industryJson;

    private ArrayList<TextView> textViews = new ArrayList<>();
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findView();
        addAction();

        boolean isPay = getIntent().getBooleanExtra("isPay",false);
        if (isPay){
            addHeader();
            setTitle("");
            StatusBarUtil.setStatusBarColor(this,R.color.app_color_main);
            StatusBarUtil.setLightStatusBar(this,false,false);
        }
    }

    void addHeader(){
        View view = LayoutInflater.from(mContext).inflate(R.layout.address_header,null,false);
        LinearLayout layout = findViewByTag("layout");
        layout.addView(view,0);
    }

    void findView(){
        FirstName = findViewByTag("FirstName");
        FirstNameInput = findViewByTag("FirstNameInput");

        LastName = findViewByTag("LastName");
        LastNameInput = findViewByTag("LastNameInput");

        Phone = findViewByTag("Phone");
        PhoneInput = findViewByTag("PhoneInput");

        Gender = findViewByTag("Gender");
        GenderInput = findViewByTag("GenderInput");

        Birth = findViewByTag("Birth");
        BirthInput = findViewByTag("BirthInput");

        Industry = findViewByTag("Industry");
        IndustryInput = findViewByTag("IndustryInput");

        Region = findViewByTag("Region");
        RegionInput = findViewByTag("RegionInput");

        City = findViewByTag("City");
        CityInput = findViewByTag("CityInput");

        Address = findViewByTag("Address");
        AddressInput = findViewByTag("AddressInput");

        CheckBox = findViewByTag("checkbox");

        submit = findViewByTag("submit");

        textViews.add(FirstName);
        textViews.add(LastName);
        textViews.add(Phone);
        textViews.add(Gender);
        textViews.add(Birth);
        textViews.add(Industry);
        textViews.add(Region);
        textViews.add(City);
        textViews.add(Address);
        addTextColor();
    }

    void addAction(){
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
        GenderInput.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                setGender_select();
            }
        });
        BirthInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setBirth();
            }
        });

        IndustryInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectIndustry();
            }
        });
        RegionInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectProvince();
            }
        });
        CityInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectCity();
            }
        });

        CheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                default_address = !default_address;
                if (default_address){
                    CheckBox.setBackground(getResources().getDrawable(R.drawable.login_checkbox_selected));
                }else {
                    CheckBox.setBackground(getResources().getDrawable(R.drawable.login_checkbox_default));
                }
            }
        });
    }

    void submit(){
//        Bid.SetDefault setDefault = Bid.SetDefault.SD_BY_DEFAULT;
//        if (!default_address){
//            setDefault = Bid.SetDefault.SD_NONE;
//        }
//        Bid.SetExpressAddressRequest item = Bid.SetExpressAddressRequest.newBuilder()
//                .setFirstName(FirstNameInput.getText().toString())
//                .setLastName(LastNameInput.getText().toString())
//                .setPhone(PhoneInput.getText().toString())
//                .setCity(city)
//                .setState(province)
//                .setDefault(setDefault)
//                .setAddress(AddressInput.getText().toString())
//                .build();
//        GBNetRequest.stubUser().setExpressAddress(item, ServerResponseObserver.observer(new ServerResponseObserver.requestListener() {
//            @Override
//            public void complete(Object value, GBError error) {
//                if (error == null){
//                    Intent intent = new Intent(mContext,this.getClass());
//                    setResult(1,intent);
//                    finish();
//                }
//            }
//        }));
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    void setGender_select(){
        GenderSelect select = new GenderSelect();
        select.listener = new GenderSelect.confirmListener() {
            @Override
            public void selectGender(Common.Gender gender) {
                gender_select = gender;
                switch (gender){
                    case MALE:
                        GenderInput.setText("MALE");
                        break;
                    case FEMALE:
                        GenderInput.setText("FEMALE");
                        break;
                }
            }
        };
        select.show(getSupportFragmentManager(),"select_gender");
    }

    void setBirth(){
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Log.d("TAG", "onTimeSelect: ");
                BirthInput.setText(GBTimeManager.getInstance().format(date));
                birthDay = String.valueOf(date.getTime());
            }
        }).build();
        Button cancel = (Button)pvTime.findViewById(R.id.btnCancel);
        Button submit = (Button)pvTime.findViewById(R.id.btnSubmit);

        cancel.setBackgroundColor(Color.TRANSPARENT);
        submit.setBackgroundColor(Color.TRANSPARENT);

        cancel.setTextColor(getResources().getColor(R.color.app_color_main));
        submit.setTextColor(getResources().getColor(R.color.app_color_main));
        pvTime.show();
    }

    public HashMap getCityJson() {
        if (cityJson == null){
            String city_Json = GBUtil.getJson("city.json",mContext);
            Gson gson = new Gson();
            HashMap city = gson.fromJson(city_Json, HashMap.class);
            cityJson = city;
        }
        return cityJson;
    }

    public ArrayList<String> getIndustryJson() {
        if (industryJson == null){
            String Json = GBUtil.getJson("industry.json",mContext);
            Gson gson = new Gson();
            ArrayList<String> List = gson.fromJson(Json, ArrayList.class);
            industryJson = List;
        }
        return industryJson;
    }

    public ArrayList<String> getProvinceJson() {
        if (provinceJson == null){
            String province_Json = GBUtil.getJson("province.json",mContext);
            Gson gson = new Gson();
            ArrayList<String> province = gson.fromJson(province_Json, ArrayList.class);
            provinceJson = province;
        }
        return provinceJson;
    }

    void selectProvince(){
        ArrayList<String> province_Json = getProvinceJson();
        final ArrayList<String> options1Items = new ArrayList<>();
        options1Items.addAll(province_Json);

        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                Log.d("TAG", "onOptionsSelect: ");
                province = options1Items.get(options1);
                RegionInput.setText(province);
            }
        }).build();
        Button cancel = (Button)pvOptions.findViewById(R.id.btnCancel);
        Button submit = (Button)pvOptions.findViewById(R.id.btnSubmit);

        cancel.setBackgroundColor(Color.TRANSPARENT);
        submit.setBackgroundColor(Color.TRANSPARENT);

        cancel.setTextColor(getResources().getColor(R.color.app_color_main));
        submit.setTextColor(getResources().getColor(R.color.app_color_main));
        pvOptions.setPicker(options1Items);
        pvOptions.show();
    }

    void selectIndustry(){
        ArrayList<String> Json = getIndustryJson();
        final ArrayList<String> options1Items = new ArrayList<>();
        options1Items.addAll(Json);

        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                Log.d("TAG", "onOptionsSelect: ");
                industry = options1Items.get(options1);
                IndustryInput.setText(industry);
            }
        }).build();
        Button cancel = (Button)pvOptions.findViewById(R.id.btnCancel);
        Button submit = (Button)pvOptions.findViewById(R.id.btnSubmit);

        cancel.setBackgroundColor(Color.TRANSPARENT);
        submit.setBackgroundColor(Color.TRANSPARENT);

        cancel.setTextColor(getResources().getColor(R.color.app_color_main));
        submit.setTextColor(getResources().getColor(R.color.app_color_main));
        pvOptions.setPicker(options1Items);
        pvOptions.show();
    }

    void selectCity(){
        HashMap<String,ArrayList<String>> city_Json = getCityJson();
        ArrayList<String> province_Json = getProvinceJson();

        final ArrayList<String> options1Items = new ArrayList<>();
        options1Items.addAll(province_Json);

        final ArrayList<ArrayList<String>> options2Items = new ArrayList<>();

        for (String key:options1Items) {
            ArrayList<String> js = city_Json.get(key);
            options2Items.add(js);
        }

        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                Log.d("TAG", "onOptionsSelect: ");
                city = options2Items.get(options1).get(option2);
                CityInput.setText(city);
            }
        }).build();
        Button cancel = (Button)pvOptions.findViewById(R.id.btnCancel);
        Button submit = (Button)pvOptions.findViewById(R.id.btnSubmit);

        cancel.setBackgroundColor(Color.TRANSPARENT);
        submit.setBackgroundColor(Color.TRANSPARENT);

        cancel.setTextColor(getResources().getColor(R.color.app_color_main));
        submit.setTextColor(getResources().getColor(R.color.app_color_main));

        pvOptions.setPicker(options1Items,options2Items);
        pvOptions.show();
    }

    void addTextColor(){
        for (TextView textView : textViews){
            textView.setText(GBUtil.textAddColor(textView.getText().toString(),getResources().getColor(R.color.app_color_main),0,1));
        }
    }
}